define(function(require, exports, module) {
  'use strict';

  var Confirm = require('confirm');

  var Search = require('../common/search/search');
  var Select = require('../common/select/select');
  var io = require('../common/io');

  var opts = [
    '<a href="javascript:;" data-id="{{id}}" data-role="view">详情</a>',
    '<a href="javascript:;" data-id="{{id}}" data-role="freeze" data-name="{{name}}">冻结</a>',
    '<a href="javascript:;" data-role="del">删除</a>'
  ];
  var search = new Search({
    url: 'userList',
    delUrl: 'userUpdateStatus',
    keepHistory: false,
    dateCfg: {
      disabled: true
    },
    formCfg: {
      data: {
        groups: [{
          // label: '页面名称',
          colspan: null,
          attrs: {
            type: 'text',
            name: 'name',
            maxlength: 30,
            placeholder: '用户名'
          }
        }, {
          // label: '所属模板',
          colspan: null,
          attrs: {
            type: 'select',
            name: 'status',
            placeholder: '状态'
          },
          options: [{
            value: '',
            text: '全部'
          }, {
            value: '10',
            text: '冻结'
          }, {
            value: '00',
            text: '正常'
          }]
        }, {
          colspan: null,
          value: '<i class="fa fa-search"></i> 搜索',
          attrs: {
            type: 'submit'
          }
        }]
      },
      events: {
        render: function() {
          console.log(this.$('[name=status]'));
        }
      }
    },
    gridCfg: {
      columns: [{
        key: 'id',
        name: '序号'
      }, {
        key: 'name',
        name: '用户名'
      }, {
        key: 'level',
        name: '用户等级'
      }, {
        key: 'status',
        name: '状态',
        template: '{{{statusName status}}}'
      }, {
        key: 'docCreateTime',
        name: '注册时间'
      }, {
        key: 'vitNum',
        name: '活跃度'
      }, {
        name: '操作',
        template: opts.join('')
      }]
    },
    gridHelpers: {
      statusName: function(status) {
        var STATUS = {
          '00': '<span class="label label-primary">冻结</span>',
          '10': '<span class="label label-danger">正常</span>'
        };
        return STATUS[status || '10'];
      }
    },
    events: {
      render: function() {

      }
    },
    delegates: {
      'click [data-role=view]': function() {
        window.location.href = '/admin/user/view.html';
      },
      'click [data-role=freeze]': function(e) {
        var self = this;
        var dataset = e.currentTarget.dataset;
        var params = {
          docId: dataset.id,
          docStatus: '10'
        };
        var name = dataset.name;
        new Confirm({
          content: '确定冻结 ' + name + ' 用户吗？'
        }).submit(function() {
          io.post('userUpdateStatus', params, function() {
            self.refresh();
          });
        });

      }
    }
  });

  new Select({
    field: search.$('[name=status]')
  });
});
